package com.example.otto.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.otto.entity.Manager;
import com.example.otto.mapper.ManagerMapper;
import com.example.otto.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manager")
public class ManagerController {

//    @Autowired
//    private ManagerMapper managerMapper;

    @Autowired
    private ManagerService managerService;

    @PostMapping
    public boolean save(@RequestBody Manager manager){
        return managerService.saveManager(manager);
    }

    @GetMapping
    public List<Manager> findAll() {
        return managerService.list();
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        return managerService.removeById(id);
    }

//    @GetMapping("/page")
//    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
//        pageNum = (pageNum-1) * pageSize;
//        List<Manager> data = managerMapper.selectPage(pageNum,pageSize);
//        Integer total = managerMapper.selectTotal();
//        Map<String,Object> res = new HashMap<>();
//        res.put("data",data);
//        res.put("total",total);
//        return res;
//    }

    //plus分页
    @GetMapping("/page")
    public IPage<Manager> findPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "", value = "username", required = false) String username,
                                   @RequestParam(defaultValue = "", value = "nickname", required = false) String nickname ){
        IPage<Manager> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", username);
        queryWrapper.like("nickname", nickname);
        return managerService.page(page, queryWrapper);
    }

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Manager manager){
          return managerService.loginService(manager);
    }


}
